package com.example.match.camera;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;


import com.example.match.R;
import com.example.mylibrary.MyApp;
import com.example.mylibrary.config.Config;
import com.example.mylibrary.data.ByteImg;
import com.example.mylibrary.util.Logger;

import java.util.ArrayList;


public class FragmentCamera extends Fragment {
    CameraSurfaceView surfaceView = null;
    private ImageButton shutterBtn;
    private ImageButton switchBtn;
    private ImageView faceImg;
    private TextView tv_switch_type;
    private Logger logger;
    float previewRate = -1f;
    private View view;

    private Context context;
    private ViewGroup.LayoutParams params;
    private FacePicInterface inter;
    private int mode_count;
    private MyApp app;
    private FaceView faceView;
    private byte[] IDCardData = null;
    private ArrayList<ByteImg>bytes;


    public ArrayList<ByteImg> getBytes() {
        return bytes;
    }

    public void setBytes(ArrayList<ByteImg> bytes) {
        this.bytes = bytes;
    }

    public FaceView getFaceView() {
        return faceView;
    }

    public void setFaceView(FaceView faceView) {
        this.faceView = faceView;
    }

    public byte[] getIDCardData() {
        return IDCardData;
    }

    public void setIDCardData(byte[] IDCardData) {
        this.IDCardData = IDCardData;
    }
    @Override
    public void onAttach(Activity activity) {
        inter = (FacePicInterface) activity;
        app = MyApp.getInstance();
        super.onAttach(activity);
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_camera1, container, false);
        context = getActivity();
        logger = new Logger();
        logger.setTag(FragmentCamera.class.getSimpleName());
        initUI();
        initViewParams();
        shutterBtn.setOnClickListener(new BtnListeners());
        switchBtn.setOnClickListener(new BtnListeners());
        // shutter mode : auto, manual
        if (app.getPreferences().getInt(Config.KEY_SHUTTER_MODE,
                Config.MODE_AUTO) == Config.MODE_AUTO) {
            shutterBtn.setEnabled(false);
            shutterBtn.setVisibility(View.GONE);
            faceImg.setVisibility(View.GONE);
        } else {
            shutterBtn.setEnabled(true);
            shutterBtn.setVisibility(View.VISIBLE);
            faceImg.setVisibility(View.VISIBLE);
        }



        return view;
    }


    @Override
    public void onResume() {
        super.onResume();
        app.faceDetectRectShowCount = 0;
    }

    @Override
    public void onPause() {
        super.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        inter.fromPic(222, 0, null);

    }



    private void initUI() {
        surfaceView = (CameraSurfaceView) view
                .findViewById(R.id.camera_surfaceview);
        shutterBtn = (ImageButton) view.findViewById(R.id.btn_shutter);
        switchBtn = (ImageButton) view.findViewById(R.id.btn_switch);
        faceView = (FaceView) view.findViewById(R.id.face_view);
        faceImg = (ImageView) view.findViewById(R.id.face_img);
        tv_switch_type = (TextView) view.findViewById(R.id.tv_switch_type);
        int shutterMode = app.getPreferences().getInt(Config.KEY_SHUTTER_MODE,
                Config.MODE_AUTO);
        if (shutterMode == Config.MODE_AUTO)
            tv_switch_type.setText("自动拍照");
        else
            tv_switch_type.setText("手动拍照");
    }

    private void initViewParams() {
        params = surfaceView.getLayoutParams();
        params.width = Config.WIDTH;
        params.height = Config.HEIGHT;
        surfaceView.setLayoutParams(params);
    }

    private class BtnListeners implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.btn_shutter:
                    takePicture();
                    break;
                case R.id.btn_switch:
                    switchCameraMode();
                    break;
                default:
                    break;
            }
        }
    }
    /**
     * 切换拍照模式
     */
    private void switchCameraMode() {
        int currentShutterMode = app.getPreferences().getInt(
                Config.KEY_SHUTTER_MODE, Config.MODE_AUTO);
        app.getPreferences()
                .edit()
                .putInt(Config.KEY_SHUTTER_MODE,
                        (currentShutterMode == Config.MODE_AUTO) ? Config.MODE_MANUAL
                                : Config.MODE_AUTO).commit();
        if (app.getPreferences().getInt(Config.KEY_SHUTTER_MODE,
                Config.MODE_AUTO) == Config.MODE_AUTO) {
            shutterBtn.setEnabled(false);
            shutterBtn.setVisibility(View.GONE);
            faceImg.setVisibility(View.GONE);
            tv_switch_type.setText("自动拍照");
        } else {
            shutterBtn.setEnabled(true);
            shutterBtn.setVisibility(View.VISIBLE);
            faceImg.setVisibility(View.VISIBLE);
            tv_switch_type.setText("手动拍照");
        }
    }
    /**
     * 拍照
     */
    private void takePicture() {
        CameraInterface.getInstance(context).doTakePicture();
    }


}
